<?php 
require_once ("mysql.class.php");
class Catalog {
    private static $instance = null;
    private $pagesize = 30;
    
    private $mysql = null;
    
    private function __construct() {
        $this->mysql = new Mysql();
    }
    
    public static function getInstance() {
        if (Catalog::$instance == null) {
            Catalog::$instance = new Catalog();
        }
        
        return Catalog::$instance;
    }

    
    public function getListFile($page, $order, $words_arr) {
        $order_query = "";
        switch ($order) {
            case "nd":
                $order_query = "filename DESC";
                break;
            case "na":
                $order_query = "filename ASC";
                break;
            case "dsa":
                $order_query = "datestart ASC";
                break;
            case "dsd":
                $order_query = "datestart DESC";
                break;
            case "dsa":
                $order_query = "datestart ASC";
                break;
            case "ded":
                $order_query = "dateend DESC";
                break;
            case "dea":
                $order_query = "dateend ASC";
                break;
            case "dfd":
                $order_query = "destinationfolder DESC";
                break;
            case "dfa":
                $order_query = "destinationfolder ASC";
                break;
            default:
                $order_query = "filename ASC";
                break;
        }
        
        $words_query = "";
        for ($i = 0; $i < count($words_arr); $i++) {
            if (! empty($words_query)) {
                $words_query .= " AND ";
            }
            $words_query .= "filename LIKE '%".$words_arr[$i]."%'";
        }
        
        if (! empty($words_query)) {
            $words_query = "WHERE ".$words_query;
        }

        
        $this->mysql->connect();
        //echo sprintf("SELECT DISTINCT * FROM files_catalog ORDER BY %s LIMIT %s,%s", $order_query, (($page-1) * $this->pagesize), $this->pagesize);
        $result = $this->mysql->query(sprintf("SELECT DISTINCT * FROM files_catalog %s ORDER BY %s LIMIT %s,%s", $words_query, $order_query, (($page - 1) * $this->pagesize), $this->pagesize));
        
        $collection = array();
        while ($record = $result->fetch_array(MYSQLI_ASSOC)) {
            $record["downloaded"] = $this->exists($record);
            $record["downloading"] = $this->downloadProgress($record);
            $record["status"] = $record["downloading"] ? 3 : $record["downloaded"];
            
            $collection[$record["id"]] = $record;
        }
        $this->mysql->disconnect();
        
        return $collection;
    }
    
    public function exists($record) {
        if (! empty($record["filename"])) {
            $store1 = "/store1/shared/";
            $store2 = "/store1/shared/";
            $storecache_path = "/store_cache/amule/Incoming/".$record["filename"];
            $path = $record["destinationpath"]."/".(! empty($record["new_filename"]) ? $record["new_filename"] : $record["filename"]);
            
            return file_exists($store1.$path) || file_exists($store2.$path) ? 2 : (file_exists($storecache_path) ? 1 : 0);
        }
        return 0;
    }
    
    public function downloadProgress($record) {
        $md5file = $this->getMD5FromLink($record["url"]);
        return ! empty($md5file) ? strpos($this->getAmuleDownloadStatus(), $md5file) : false;
    }
    
    public function getAmuleDownloadStatus() {
        return shell_exec("amulecmd -P=elettrodo1254 -c=\"Show DL\"");
    }
    
    public function getMD5FromLink($ed2klink) {
        //ed2k://|file|[Wii]Super_Smash_Bros_Brawl[DVD5][PAL].rar|4614106199|9AB1CFC952460AA74375F7991CEB6C97|/
        $md5file = "";
        if (! empty($ed2klink)) {
            $link_parts = explode("|", $ed2klink);
            if (count($link_parts) >= 5) {
                $md5file = $link_parts[4];
            }
        }
        
        return $md5file;
    }
}
?>
